/**
 * Created by cxj on 2016/2/22.
 */
var grid;
define([
    "jquery",
    "IO",
    "bootbox"
], function($,IO,bootbox) {
    $(function () {

        //每列数据列性
        var columns = [
            {
                title: '课程名称',
                dataIndex: 'course_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+data[datarow].course_name+"</span>";
                }
            },
            {
                title: '题目类型',
                dataIndex: 'question_type',
                dataSource: function (data, datarow, gridobj, current_column) {
                    if(data[datarow].type==0)
                        return "<span>判断题</span>";
                    if(data[datarow].type==1)
                        return "<span>单选题</span>";
                    if(data[datarow].type==2)
                        return "<span>多选题</span>";
                }
            },
            {
                title: '题目详情',
                dataIndex: 'question',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>"+data[datarow].title+"</span>";
                }
            },
            {
                title: '操作',
                dataIndex: 'action',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span><a href='javascript:void(0)' style='color: red;' class='delete' act_id='"+data[datarow].question_id+"'>删除</a>/<a href='/pte/editquestion?question_id="+data[datarow].question_id+"' style='color: orange;'>编辑</a></span>";
                }
            },

        ];


        //数组初始化

        function dataInit() {
            grid = new Grid("/pte/api/question", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 10);

            //初始化
            grid.Init();
        }
        //获取输入框内容
        function get_input_param() {
            var type=$("#category option:selected").attr("data-id");
            var title=$("#que_title").val();
            var param="type=" + type +"&title=" +title;
            return param;
        }
        dataInit();
        //查询
        $("#query").bind("click", function () {
            dataInit();
        });
    });
//删除题目
    $(document).on("click",".delete",function(){
        var act_id=$(this).attr("act_id");
        if(act_id){
            bootbox.confirm("确定要删除该题？",function(result){
                if(result) {
                    IO.get("act/question_delete",{act_id:act_id},function(d){
                        bootbox.alert(d.msg,function(){
                            if (d.code == 0) {
                                location.href = '/pte/question';
                            }
                        })
                    });
                }
            });
        }
    });
});